package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.glowmee.cmd.HotEditerCMD;
import com.glowdayz.glowmee.dao.HotEditerDAO;

public class HotProductBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public HotProductBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		if (order != null && order.equals("isUnDisplay")) {
			order = "h.isDisplay ASC, h.create_date DESC";
			request.setAttribute("order", "&order=isUnDisplay");
		} else if (order != null && order.equals("isDisplay")) {
			order = "h.isDisplay DESC, h.create_date DESC";
			request.setAttribute("order", "&order=isDisplay");
		}
		
		String idProduct_str = request.getParameter("pId");
		String idSecondCategory_str = request.getParameter("sId");
		String hotEditerType = request.getParameter("type");
		String isDisplay_str = request.getParameter("isDisplay");
		if (idProduct_str != null && idSecondCategory_str != null && hotEditerType != null && isDisplay_str != null) {
			int idProduct = Integer.parseInt(idProduct_str);
			int idSecondCategory = Integer.parseInt(idSecondCategory_str);
			int isDisplay = Integer.parseInt(isDisplay_str);
			
			if (isDisplay == 1) {
				isDisplay = 0;
			} else if (isDisplay == 0) {
				isDisplay = 1;
			}
			
			HotEditerCMD cmd = new HotEditerCMD(idProduct, idSecondCategory, hotEditerType, isDisplay);
			try {
				HotEditerDAO.getInstance().isDisplayHotEditer(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("hotProduct", HotEditerDAO.getInstance().getHotEditerList(page, 10, order, "h.hotEditerType='HOT'"));
			request.setAttribute("display", DBHandler.getInstance().getCount("hotediter", "*", "hotEditerType='HOT' AND isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("hotediter", "*", "hotEditerType='HOT' AND isDisplay='0'"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/hotProduct.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String isTop_str = request.getParameter("isTop");
		int isTop = Integer.parseInt(isTop_str);
		String secondCategory_str = request.getParameter("secondCategory");
		int secondCategory = Integer.parseInt(secondCategory_str);
		String product_str = request.getParameter("product");
		int product = Integer.parseInt(product_str);
		String promotionText = request.getParameter("promotionText");
		String start_date = request.getParameter("start_date");
		start_date = start_date.replace("-", "") + "000000";
		String end_date = request.getParameter("end_date");
		end_date = end_date.replace("-", "") + "235959";
		
		try {
			int count = DBHandler.getInstance().getCount(
					"hotediter", "*", "hotEditerType='HOT' AND idProduct=" + product + " AND idSecondCategory=" + secondCategory);
			if (count > 0) {
				response.setContentType("text/html; charset=utf-8");
				PrintWriter out = response.getWriter();
				out.print("<script type='text/javascript'>alert('동일한 화제의 제품이 추가될수 없습니다.\\n다시 선택해 주세요.');history.back();</script>");
				return;
			}
			HotEditerDAO.getInstance().insertHotProduct(new HotEditerCMD(product, secondCategory, "HOT", promotionText, isTop, start_date, end_date));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("hotProduct", HotEditerDAO.getInstance().getHotEditerList(page, 10, null, "h.hotEditerType='HOT'"));
			request.setAttribute("display", DBHandler.getInstance().getCount("hotediter", "*", "hotEditerType='HOT' AND isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("hotediter", "*", "hotEditerType='HOT' AND isDisplay='0'"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/hotProduct.jsp");
		dpr.forward(request, response);
	}

}